package persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.UsuarioEvento;

public class PersistenceUsuarioEvento {
	public static void inserirUsuarioEvento(UsuarioEvento usuario) {
		Connection con = Conexao.getConexao();
		PreparedStatement ps;
		String id_usuario = null;
		String id_evento = null;
		try {

			String querySelectUser = "SELECT * FROM usuario WHERE login_usuario = ? ";
			ps = con.prepareStatement(querySelectUser);
			ps.setString(1, usuario.getUsuario());
			ResultSet rsUser = ps.executeQuery();

			
			while (rsUser.next()) {
				id_usuario = rsUser.getString(1);
			}

			String querySelectEvent = "SELECT * FROM eventos WHERE nome_evento = ? ";
			ps = con.prepareStatement(querySelectEvent);
			ps.setString(1, usuario.getNomeEvento());
			ResultSet rsEvent = ps.executeQuery();

			while (rsEvent.next()) {
				id_evento = rsEvent.getString(1);
			}

			String query = "INSERT INTO participante_evento VALUES ("+id_evento+","+id_usuario+")";
			ps = con.prepareStatement(query);

			int status = ps.executeUpdate();
			System.out.println(status);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
